import bluepy
import uix
import uthread
import trinity
import xtriui
import blue
import uicls
import uiconst
NEUTRAL_COLOR = (1.0, 1.0, 1.0, 0.5)
HIGHLIGHT_COLOR = (1.0, 1.0, 1.0, 0.5)

class MapLabel(uicls.Container):
    __guid__ = 'xtriui.MapLabel'

    def init(self):
        self.state = uix.UI_PICKCHILDREN
        self.preFullAlpha = None



    def Startup(self, name, itemID, typeID, tracker, cloud, mylocation = None):
        self.trackTransform = tracker
        self.sr.cloud = cloud
        self.sr.id = itemID
        self.sr.typeID = typeID
        self.name = name
        if (name in ('', 'myDest', 'myloc')):
            return 
        else:
            frame = None
            if (itemID > 0):
                if (typeID == const.typeConstellation):
                    label = xtriui.Child(uicls.Label(text=(('<b>' + name) + '</b>'), parent=self, left=154, top=4, fontsize=12, uppercase=1, state=uiconst.UI_NORMAL))
                    frame = uicls.Frame(parent=self, align=uiconst.TOPLEFT, left=(label.left - 5), width=(label.width + 10), top=(label.top - 3), height=(label.height + 6))
                else:
                    label = xtriui.Child(uicls.Label(text=name, parent=self, left=154, top=7, letterspace=1, fontsize=9, uppercase=1, state=uiconst.UI_NORMAL))
            else:
                label = xtriui.Child(uicls.Label(text=name, parent=self, left=154, top=7, letterspace=1, fontsize=9, uppercase=1, state=uiconst.UI_NORMAL))
            if not mylocation:
                if (self.sr.id < 0):
                    label.color.SetRGB(1.0, 0.96, 0.78, 1.0)
            if (typeID != const.typeSolarSystem):
                label.left = ((self.width - label.width) / 2)
            if frame:
                frame.left = (label.left - 5)
                frame.width = (label.width + 10)
                frame.top = (label.top - 3)
                frame.height = (label.height + 6)
            label.state = uix.UI_NORMAL
            return self



    def _OnMouseEnter(self, *args):
        return 
        self.ShowFullAlpha()
        if (self.sr.id > 0):
            sm.GetService('starmap').UpdateLines(self.sr.id)



    def ShowFullAlpha(self):
        if (self.preFullAlpha is None):
            self.preFullAlpha = self.children[0].color.a
        self.children[0].color.a = 1.0



    def _OnMouseExit(self, *args):
        return 
        if (self.sr.id > 0):
            if (self.sr.typeID == const.typeConstellation):
                sm.GetService('starmap').UpdateLines(None)
        self.ResetFullAlpha()



    def ResetFullAlpha(self):
        if (self.preFullAlpha is not None):
            self.children[0].color.a = self.preFullAlpha
            self.preFullAlpha = None



    def _OnClick(self, *args):
        sm.GetService('starmap').SetInterest(self.sr.id)



    def GetMenu(self):
        starmap = sm.GetService('starmap')
        m = []
        if (self.sr.id >= 0):
            m += starmap.GetItemMenu(self.sr.id)
        else:
            m.append((self.name,
             sm.GetService('info').ShowInfo,
             (self.sr.typeID, self.sr.id)))
            m.append((mls.UI_CMD_CENTERONSCREEN,
             starmap.SetInterest,
             (self.sr.id, 1)))
        return m



    def _OnMouseWheel(self, *etc):
        lib = eve.triapp.uilib
        camera = sm.GetService('sceneManager').GetRegisteredCamera('starmap')
        if hasattr(camera, 'translationCurve'):
            targetTrans = (camera.translationCurve.GetVectorAt(blue.os.GetTime(1)).z * (1.0 + (-lib.dz * 0.001)))
            if (targetTrans < 2.0):
                targetTrans = 2.0
            if (targetTrans > 10000.0):
                targetTrans = 10000.0
            camera.translationCurve.keys[1].value.z = targetTrans
        uthread.new(sm.GetService('starmap').CheckLabelDist)




class Child(uicls.Container):
    __guid__ = 'xtriui.Child'

    def _OnMouseEnter(self, *etc):
        self.parent._OnMouseEnter(*etc)



    def _OnMouseExit(self, *etc):
        self.parent._OnMouseExit(*etc)



    def _OnClick(self, *etc):
        self.parent._OnClick(*etc)



    def _OnMouseWheel(self, *etc):
        self.parent._OnMouseWheel(self, *etc)



    def GetMenu(self, *etc):
        return self.parent.GetMenu(*etc)




class TransformableLabel(object):
    __guid__ = 'xtriui.TransformableLabel'
    __persistvars__ = ['shader']

    def __init__(self, text, parent, size = 72, shadow = 0, hspace = 8):
        self.transform = trinity.EveTransform()
        self.transform.mesh = trinity.Tr2Mesh()
        self.transform.mesh.geometryResPath = 'res:/Model/Global/zsprite.gr2'
        self.transform.modifier = 1
        area = trinity.Tr2MeshArea()
        self.transform.mesh.transparentAreas.append(area)
        area.effect = effect = trinity.Tr2Effect()
        effect.effectFilePath = 'res:/Graphics/Effect/Managed/Space/SpecialFX/TextureColor.fx'
        diffuseColor = trinity.TriVector4Parameter()
        diffuseColor.name = 'DiffuseColor'
        effect.parameters.append(diffuseColor)
        self.diffuseColor = diffuseColor
        diffuseMap = trinity.TriTexture2DParameter()
        diffuseMap.name = 'DiffuseMap'
        effect.resources.append(diffuseMap)
        parent.children.append(self.transform)
        kwargs = {'fontsize': size,
         'align': uiconst.TOPLEFT,
         'autowidth': 1,
         'autoheight': 1,
         'state': uiconst.UI_NORMAL,
         'letterspace': hspace,
         'uppercase': 1}
        if not shadow:
            kwargs['shadow'] = []
        textObject = uicls.Label(text=(('<b>' + text) + '</b>'), parent=None, **kwargs)
        textObject.miplevels = 3
        textObject.Render()
        textRes = textObject.texture.pixelBuffer
        self.transform.scaling = (float(textRes.width),
         float(textRes.height),
         0.0)
        diffuseMap.SetResource(textRes)
        effect.RebuildCachedData()
        textObject.Close()



    def SetDiffuseColor(self, color):
        self.diffuseColor.value = color



    def SetHighlight(self, highlight):
        if highlight:
            self.diffuseColor.value = HIGHLIGHT_COLOR
        else:
            self.diffuseColor.value = NEUTRAL_COLOR



    def SetDisplay(self, display):
        self.transform.display = display




